package com.ruoyi.hotel.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
import com.ruoyi.hotel.config.enumConfig.SuffixEnum;
import com.ruoyi.hotel.service.Query;


import java.lang.reflect.Field;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DBUtil {

    private static List<String> suffix = EnumSet.allOf(SuffixEnum.class).stream().map(SuffixEnum::getCode).collect(Collectors.toList());

    // 动态加载传入参数条件
    // 获取到全部参数和对应的值
    public static <T> void autoQuery(QueryWrapper<T> queryWrapper, Query query, Class<T> clazz) {
        Field[] fields = ReflectUtil.getFields(query.getClass());
        for (Field field : fields) {
            Object fieldValue = ReflectUtil.getFieldValue(query, field);
            String fieldName = ReflectUtil.getFieldName(field);
            if (ObjectUtil.isNotEmpty(fieldValue)) {
                analysis(queryWrapper, fieldName, fieldValue, clazz);
            }
        }
    }

    // 根据参数列表解析后缀和解析数据库字段值
    public static <T> void analysis(QueryWrapper<T> queryWrapper, String queryValue, Object fieldValue, Class<T> clazz) {
        Map<String, ColumnCache> columnMap = LambdaUtils.getColumnMap(clazz);
        // 判断后缀是否是枚举的，如果不是就默认eq
        List<String> collect = suffix.stream().filter(item -> {
            return StrUtil.endWith(queryValue, item);
        }).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(collect)) {
            String suffixValue = collect.get(0).toUpperCase();
            String attribute = StrUtil.subBefore(queryValue.toUpperCase(), suffixValue, false);
            String column = columnMap.get(attribute).getColumn();
            fun(queryWrapper, column, fieldValue, suffixValue);
        } else if(columnMap.containsKey(queryValue.toUpperCase())){
            fun(queryWrapper, columnMap.get(queryValue.toUpperCase()).getColumn(), fieldValue, "EQ");
        }

    }


    // 根据后缀，添加queryWrapper
    /***
     * 需要的参数：queryWrapper,数据库字段值，query字段值，后缀
     *
     *
     * @param queryWrapper
     */
    public static void fun(QueryWrapper queryWrapper, String column, Object queryValue, String suffix) {
        switch (suffix) {
            case "EQ":
                queryWrapper.eq(column, queryValue);
                break;
            case "LIKE":
                queryWrapper.like(column, queryValue);
                break;
            case "NE":
                queryWrapper.ne(column, queryValue);
                break;
            case "GE":
                queryWrapper.ge(column, queryValue);
                break;
            case "IN":
                if (queryValue instanceof String) {
                    queryWrapper.in(column, StrUtil.split((String) queryValue, ','));
                } else if (queryValue instanceof Collection) {
                    queryWrapper.in(column, (Collection) queryValue);
                }
                break;
        }

    }
}
